package com.guli.mall.pms.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.common.utils.QuickSort;
import com.guli.mall.pms.entity.vo.AttrForBindingValueAndUpdateVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.mall.pms.mapper.ProductAttrValueMapper;
import com.guli.mall.pms.entity.ProductAttrValueEntity;
import com.guli.mall.pms.service.ProductAttrValueService;
import org.springframework.util.ObjectUtils;


@Service("productAttrValueService")
public class ProductAttrValueServiceImpl extends ServiceImpl<ProductAttrValueMapper, ProductAttrValueEntity> implements ProductAttrValueService {

    @Override
    public PageUtils queryPage(Integer currentPage, Integer size) {

        Page<ProductAttrValueEntity> page = new Page<>(currentPage, size);

        Page<ProductAttrValueEntity> resultPage = this.page(page);


        return new PageUtils(resultPage);
    }

    @Override
    public ProductAttrValueEntity getAttrValueByAttrIdAndSpuId(Long attrId, Long spuId) {

        QueryWrapper<ProductAttrValueEntity> wrapper = new QueryWrapper<>();

        wrapper.eq("spu_id", spuId);

        wrapper.eq("attr_id", attrId);

        return this.getOne(wrapper, false);
    }

    @Override
    public void updateProductAttrValue(List<AttrForBindingValueAndUpdateVo> attrForBindingValueAndUpdateVoList) {


        List<ProductAttrValueEntity> attrValueList = attrForBindingValueAndUpdateVoList.stream().map(attrValueVo -> {
            ProductAttrValueEntity valueEntity = new ProductAttrValueEntity();


            BeanUtils.copyProperties(attrValueVo, valueEntity);

            // 判断如果该属性没有的话，要新增而不是更新操作
            if (ObjectUtils.isEmpty(valueEntity.getId())) {
                this.save(valueEntity);
                return null; // 新增之后返回null，再过滤，避免插入之后又更新了一次
            }

            return valueEntity;
        }).filter(valueEntity -> {
            return !ObjectUtils.isEmpty(valueEntity);
        }).collect(Collectors.toList());

        this.updateBatchById(attrValueList);

    }

    @Override
    public List<ProductAttrValueEntity> getBySpuId(Long spuId) {

        QueryWrapper<ProductAttrValueEntity> wrapper = new QueryWrapper<>();

        wrapper.eq("spu_id", spuId);

        List<ProductAttrValueEntity> list = this.list(wrapper);

        QuickSort.sort(list, (v1, v2) -> {
            return v1.getAttrSort() - v2.getAttrSort();
        });

        return list;
    }

    @Override
    public List<ProductAttrValueEntity> getAttrValueBySpuIdAndAttrIdList(Long id, List<Long> attrIdList) {

        QueryWrapper<ProductAttrValueEntity> wrapper = new QueryWrapper<>();

        wrapper.eq("spu_id", id)
                .and(w -> {
                    w.in("attr_id",attrIdList);
                });

        return this.list(wrapper);
    }

}